function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}



function getDays(date) {
  // 构造1月1日
  var curDate = new Date(date);
  var lastDay = new Date();
  lastDay.setMonth(0);
  lastDay.setDate(1);

  var year = lastDay.getFullYear() + '';
  if (date.indexOf(year) != -1) {
    // 获取距离1月1日过去多少天
    var days = (curDate - lastDay) / (1000 * 60 * 60 * 24) + 1;
    return parseInt(days) + 1;
  } else {
    lastDay.setFullYear(date.substring(0, 4));
    // 获取距离1月1日过去多少天
    var days = (curDate - lastDay) / (1000 * 60 * 60 * 24) + 1;
    return parseInt(days) + 1;
  }
}



//格式化时间
function formateDate(objTime) {
  var pattern = /(\d{4})(\d{2})(\d{2})/;
  var formated = objTime.replace(pattern, '$1-$2-$3');
  return formated;
}

function handletoLocal(date) {
  // 确保date 最终为Date object
  date = new Date(date);
  var local = date.toLocaleString('en-US', {
    hour12: false,
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  });
  return local;
};


function settime(current) {
  if (current == null || current==''){
      return '00:00';
  }

    var min = current / 60;
   
    var seco1 = current - parseInt(min) * 60;
    var seco = parseInt(seco1);
    var hs = parseInt((seco1 - seco) * 100);
    return parseInt(min) >= 10 ? parseInt(min) : "0" + parseInt(min) + ":" + (seco >= 10 ? seco : "0" + seco) ;//+ ":" + (hs >= 10 ? hs : "0" + hs);
  }


module.exports = {
  formatTime: formatTime,
  getDays:getDays,
  formateDate: formateDate,
  handletoLocal: handletoLocal,
  settime: settime
}
